package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.*;
import com.xjc.business.entity.domain.xlsx.StuImportEntity;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.coladmin.StuVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.StudentsMapper;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【students(学生表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:32
*/
@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students>
    implements StudentsService {

    @Autowired
    @Lazy
    AllStudentRegsiterRelationsService allStudentRegsiterRelationsService;

    @Autowired
    @Lazy
    MajorsService majorsService;

    @Autowired
    @Lazy
    ClazzsService clazzsService;

    @Autowired
    @Lazy
    MajorClazzRelationsService majorClazzRelationsService;

    @Autowired
    @Lazy
    SchoolCollegeRelationsService schoolCollegeRelationsService;

    /**
     * 分页查询学生信息
     *
     * @param pageSize    每页数量
     * @param currentPage 当前页码
     * @param collegeId   院校id
     * @param majorId     专业id
     * @param classId     班级id
     * @return 数据
     */
    @Override
    public Result<PageVo<StuVo>> getStuPage(Integer pageSize, Integer currentPage, Integer collegeId, Integer majorId, Integer classId) {
        PageVo<StuVo> stuVoPageVo = new PageVo<>();

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getColId,collegeId);
        if(majorId!=-1){
            allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getMajId,majorId);
        }
        if(classId!=-1){
            allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getClazzId,classId);
        }

        List<Integer> collectStuIdList = allStudentRegsiterRelationsService.list(allStudentRegsiterRelationsLambdaQueryWrapper).stream().map(AllStudentRegsiterRelations::getStuId).collect(Collectors.toList());

        if(collectStuIdList.isEmpty()){
            return Result.okWithData(stuVoPageVo);
        }

        LambdaQueryWrapper<Students> studentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentsLambdaQueryWrapper.in(Students::getId,collectStuIdList);

        Page<Students> studentsPage = new Page<>(currentPage, pageSize);
        page(studentsPage,studentsLambdaQueryWrapper);

        List<Students> studentsPageRecords = studentsPage.getRecords();

        List<StuVo> stuVoList = studentsPageRecords.stream().map(students -> {
            StuVo stuVo = new StuVo();
            stuVo.setId(students.getId());
            LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            allStudentRegsiterRelationsLambdaQueryWrapper2.eq(AllStudentRegsiterRelations::getStuId,students.getId());
            AllStudentRegsiterRelations one = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper2);
            stuVo.setMajorName(majorsService.getById(one.getMajId()).getName());
            stuVo.setClassName(clazzsService.getById(one.getClazzId()).getNum()+"班");
            stuVo.setSno(students.getSno());
            stuVo.setName(students.getName());
            stuVo.setSex(MyConstants.STU_SEX.get(students.getSex()));
            stuVo.setAge(students.getAge());
            stuVo.setStatus(MyConstants.STU_STATUS.get(students.getStatus()));
            stuVo.setCreateTime(students.getCreateTime());
            return stuVo;
        }).collect(Collectors.toList());

        stuVoPageVo.setData(stuVoList);
        stuVoPageVo.setTotal(studentsPage.getTotal());
        stuVoPageVo.setCurrent(currentPage);
        stuVoPageVo.setSize(pageSize);
        stuVoPageVo.setPageCount(studentsPage.getPages());

        return Result.okWithData(stuVoPageVo);
    }

    /**
     * 批量添加学生信息
     *
     * @param dataList 数据
     */
    @Override
    public void batchAddStudent(List<StuImportEntity> dataList,Integer collegeId) {

        LambdaQueryWrapper<SchoolCollegeRelations> schoolCollegeRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolCollegeRelationsLambdaQueryWrapper.eq(SchoolCollegeRelations::getColId,collegeId);
        SchoolCollegeRelations schoolCollegeRelations = schoolCollegeRelationsService.getOne(schoolCollegeRelationsLambdaQueryWrapper);
        Integer schId = schoolCollegeRelations.getSchId();

        for (StuImportEntity stuImportEntity : dataList) {
            Students students = new Students();
            students.setRegisterId(stuImportEntity.getRegisterId());
            students.setSno(stuImportEntity.getSno());
            students.setCardId(stuImportEntity.getCardId());
            students.setName(stuImportEntity.getName());
            students.setSex(stuImportEntity.getSex().equals("男")?1:0);
            students.setAge(stuImportEntity.getAge());
            students.setProvince(stuImportEntity.getProvince());
            students.setCity(stuImportEntity.getCity());
            students.setArea(stuImportEntity.getArea());
            students.setDetails(stuImportEntity.getDetails());
            students.setPhone(stuImportEntity.getPhone());
            students.setEmail(stuImportEntity.getEmail());
            students.setStatus(stuImportEntity.getStatus().equals("在籍")?1:0);
            students.setMarriedStatus(stuImportEntity.getMarriedStatus().equals("已婚")?1:0);
            students.setUpdateTime(new Date());
            students.setCreateTime(new Date());

            save(students);

            AllStudentRegsiterRelations allStudentRegsiterRelations = new AllStudentRegsiterRelations();

            allStudentRegsiterRelations.setSchId(schId);
            allStudentRegsiterRelations.setColId(collegeId);
            allStudentRegsiterRelations.setStuId(students.getId());

            LambdaQueryWrapper<Majors> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Majors::getName,stuImportEntity.getMajorName());
            Majors majors = majorsService.getOne(lambdaQueryWrapper);
            allStudentRegsiterRelations.setMajId(majors.getId());

            LambdaQueryWrapper<MajorClazzRelations> majorClazzRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            majorClazzRelationsLambdaQueryWrapper.eq(MajorClazzRelations::getMajId,majors.getId());
            List<Integer> collectIdList = majorClazzRelationsService.list(majorClazzRelationsLambdaQueryWrapper).stream().map(MajorClazzRelations::getClId).collect(Collectors.toList());

            LambdaQueryWrapper<Clazzs> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(Clazzs::getNum,stuImportEntity.getClassName());
            lambdaQueryWrapper2.in(Clazzs::getId,collectIdList);

            Clazzs clazzs = clazzsService.getOne(lambdaQueryWrapper2);
            allStudentRegsiterRelations.setClazzId(clazzs.getId());
            allStudentRegsiterRelationsService.save(allStudentRegsiterRelations);
        }
    }

    /**
     * 获取学生列表
     * @param classId 班级id
     * @return 列表
     */
    @Override
    public Result<List<com.xjc.business.entity.vo.classadmin.StuVo>> getStuList(Integer classId) {
        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getClazzId,classId);
        List<Integer> collectIdList  = allStudentRegsiterRelationsService.list(allStudentRegsiterRelationsLambdaQueryWrapper).stream().map(AllStudentRegsiterRelations::getStuId).collect(Collectors.toList());
        if(collectIdList.isEmpty()){
            return Result.okWithData(new ArrayList<>());
        }
        LambdaQueryWrapper<Students> studentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentsLambdaQueryWrapper.in(Students::getId,collectIdList);
        List<Students> studentsList = list(studentsLambdaQueryWrapper);
        List<com.xjc.business.entity.vo.classadmin.StuVo> stuVoList = studentsList.stream().map(students -> {
            com.xjc.business.entity.vo.classadmin.StuVo stuVo = new com.xjc.business.entity.vo.classadmin.StuVo();
            stuVo.setId(students.getId());
            stuVo.setSno(students.getSno());
            stuVo.setRegisterId(students.getRegisterId());
            stuVo.setName(students.getName());
            stuVo.setSex(MyConstants.STU_SEX.get(students.getSex()));
            stuVo.setAge(students.getAge());
            stuVo.setPhone(students.getPhone());
            stuVo.setStatus(MyConstants.STU_STATUS.get(students.getStatus()));
            stuVo.setCreateTime(students.getCreateTime());

            return stuVo;
        }).collect(Collectors.toList());

        return Result.okWithData(stuVoList);
    }
}




